<? include("/inc/header.html") {} ?>
<div class="page-content" style="margin-top: -15px;">
		<div class="row mbl" style="overflow: auto;height: 100%;">
			<div class="col-lg-12">
			
				<div class="portlet box">
                    <div class="portlet-body" style="height: 90%;overflow: auto;">
                    <div style="height: 120px;">
						已选择：
						  <div id="menuButtion" style="padding-left: 4%;overflow:auto;height: 100px;"></div>
					</div>
                    
                        <div class="table-responsive">
						<div id="example1_wrapper"
							class="dataTables_wrapper form-inline no-footer">
							<div class="row">
								<div class="col-md-6">
									<table style="width: 100%;">
										<tr height="50">
											<td align="right" style="width: 100px;">用户名:&nbsp;&nbsp;</td>
											<td><input type="text"  class="form-control" id="username" name="username" /></td>
											<td align="right" style="width: 100px;">手机号:&nbsp;&nbsp;</td>
											<td><input type="text"  class="form-control" id="telephone" name="telephone" /></td>
											<td align="right" style="width: 100px;"><button type="button" data-toggle="dropdown" class="btn btn-blue dropdown-toggle" onclick="get()">查询</button></td>
										</tr>
									</table>
									
									
								</div>
							</div>
							<table style="font-size: 14px;" class="table table-striped table-bordered table-hover dataTable no-footer">
								<thead>
									<tr>
										<th >序号</th>
										<th >用户名</th>
										<th >姓名</th>
										<th >昵称</th>
										<th >手机号</th>
										<th >邮箱</th>
										<th >可操作</th>
									</tr>
								</thead>
								<tbody id="findUser">
								</tbody>
							</table>
							<div id="page2" style="text-align: center; margin-top: 20px;"></div>
						</div>
					</div>
                    </div>
                    </div>
	</div>
	</div>
	</div>
		<input type="hidden" id="id" value="${id}">
		<input type="hidden" id="type" value="${type}">
		<input type="hidden" id="temp" value="">
<script type="text/javascript">
	function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
	} 
	var winName=getQueryString("winName");
	
	var curr;
	$(function()  {
		queryUser(1);
		userFind();
	});
	function get(){
		queryUser(1);
	}
	function queryUser(cur){
		$.ajax({
			type : "post",
			url : "teacherList.do",
			dataType : "json",
			data :{
				page : cur || 1,
				username:$("#username").val(),
        		telephone:$("#telephone").val()
			},
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(res){
				var data = res.result;
				var str = "";
				$("#findUser").html("");
				for ( var i = 0; i < data.length; i++) {
					str += "<tr><td>"
							+(i + 1)
							+ "</td><td> <label id=>"
							+ data[i].username
							+ "</label></td><td><label >"
							+ data[i].realName
							+ "</label></td><td><label >"
							+ data[i].nickName
							+ "</label></td><td><label >"
							+ data[i].telephone
							+ "</label></td><td><label >"
							+ data[i].email
							+ "</label></td><td>";
							if(data[i].realName==null || data[i].realName=='' 
								|| data[i].realName == undefined || data[i].realName == 'null' 
								|| data[i].realName == 'undefined'){
								str += '<button type="button" class="btn btn-default btn-xs" onclick="selectUser(\''+data[i].id+'\',\''+data[i].username+'\')">选择</button>&nbsp;';
							}else{
								str += '<button type="button" class="btn btn-default btn-xs" onclick="selectUser(\''+data[i].id+'\',\''+data[i].realName+'\')">选择</button>&nbsp;';
							}
							
							+ "</td>"
							+ "</tr>";
				}
				$("#findUser").html(str);
					laypage({
					cont : document.getElementById('page2'), //容器。值支持id名、原生dom对象，jquery对象,
					pages : res.pageSize, //总页数
					skip : true, //是否开启跳页
					skin : 'molv', //加载内置皮肤，也可以直接赋值16进制颜色值，如:#c00
					curr: curr || 1,
					last: res.pageSize,
					groups : 6,//连续显示分页数
					jump : function(obj, first) { //触发分页后的回调
						if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
							queryUser(obj.curr);
							 curr = obj.curr;
						}
					}
				});
			}
		});
	
	}
	function userFind(){
		$.ajax({
			type : "post",
			url : "./userFind.do",
			data : {
				"id" : $("#id").val(),
				"type" : $("#type").val(),
			},
			dataType : "json",
			success : function(data) {
				var html = '';
				for(var i in data){
					var name='';
					if(data[i].realName!=null 
						&& data[i].realName!=''
						&& data[i].realName!=undefined){
						name=data[i].realName;
					}else{
						name=data[i].username;
					}
					html+='<span id="'+data[i].id+'" name="'+name+'">';
					html+='<input class="btn btn-blue btn-square" style="margin-top: 20px; margin-right: 0px;" type="button" value="'+name+'">';
					html+='<a href="javascript:void(0)" onclick="removeUser(\''+data[i].id+'\')" style="font-size: 18px;"><i style="margin-left: -7px;" class="fa fa-times-circle-o"></i></a>';
					html += '</span>';
				}
				$("#menuButtion").html(html);
			},
			error : function(data) {
				alert("请求失败");
			}
		})
	}
	function selectUser(id,name){
		if($("#menuButtion").find("#"+id).attr("id")!=undefined){
			layer.msg("选择项已添加");
		}else{
			var html = $("#menuButtion").html();
			html+='<span id="'+id+'" name="'+name+'">';
			html+='<input class="btn btn-blue btn-square" style="margin-top: 20px; margin-right: 0px;" type="button" value="'+name+'">';
			html+='<a  href="javascript:void(0)" onclick="removeUser(\''+id+'\')" style="font-size: 18px;"><i style="margin-left: -7px;" class="fa fa-times-circle-o"></i></a>';
			html += '</span>';
			$("#menuButtion").html(html);
		}
	}
	
	function save(){
		var data = '';
		$("#menuButtion").find("span").each(function(){
			var id = $(this).attr("id");
			var name = $(this).attr("name");
			data+=id+':'+name+';';
		});
		console.log("1");
		console.log(data);
		console.log("2");
		var index = parent.layer.getFrameIndex(window.name);
		window.parent.window[winName].userIdsHtml(data);
		parent.layer.msg('提交成功');
		parent.layer.close(index);
	}
	function removeUser(id){
		$("#"+id).remove();
	}
</script>
